package com.mstf.cashier.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import com.mstf.cashier.utils.MathTools;
import com.mstf.cashier.utils.Time;

public class Sale_historyDAO {
	private ResultSet rs=null;
	private String sql=null;
	
	@SuppressWarnings("rawtypes")
	public void add(Vector rowDates,String userId,String customerNo){
		for(Object rowDate:rowDates){
			//((Vector) rowDate).get(1);
			 String salaTime=Time.getTime(); 
		     String no=(((Vector)rowDate).get(0)).toString();
			 int count=Integer.parseInt((((Vector)rowDate).get(4)).toString());
			 float price=Float.parseFloat((((Vector)rowDate).get(5)).toString());
			 float discount=Float.parseFloat((((Vector)rowDate).get(6)).toString());
			 
			 sql="insert sale_history values(?,?,?,?,?,?,?)";
			 Object[] params={no,count,MathTools.keepTwo(price),MathTools.keepTwo(discount),userId,salaTime,customerNo};
			 DBManager.update(sql, params);
		}
		DBManager.closeCon(null);
		
	}

	public double queryIncome(String usersName, String startTime, String endTime) {
		sql="select sum(price) from sale_history where saletime between ? and ? group by user_id having user_id=?";
		Object [] params={startTime,endTime,usersName};
		rs=DBManager.query(sql, params);
		double money=0.0;
		try {
			while(rs.next()){
				money=Double.parseDouble(rs.getString(1));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}
		return money;
	}
}